﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;

namespace Tireguit.Nmon.Parser.Information
{
    public class BBBDElement:NmonInfoElement
    {
         protected new ILog _log=LogManager.GetLogger(typeof(BBBDElement));
        protected IDataParameter _adapter_number = null;
        protected IDataParameter _name = null;
        protected IDataParameter _location = null;
        protected IDataParameter _infoline = null;
        protected IDataParameter _disks = null;
        public int AdapterNumber {get;protected set;}
        public string Name { get; protected set; }
        public string Location { get; protected set; }
        public string Disks {get; protected set;}
        public BBBDElement(IDbConnection connection)
        {
            Balise = BaliseType.BBBD;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq="INSERT INTO "+NmonTables.TablesDictionnary.Instance["BBBD"]+" ([Adapter_number],[Name],[Location],[Description],[Disks]) VALUES (@Adapter_number,@Name,@Location,@Description,@Disks)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _adapter_number = Command.CreateParameter();
            _adapter_number.ParameterName = "@Adapter_number";
            _adapter_number.DbType = DbType.Int32;
            _name = Command.CreateParameter();
            _name.ParameterName = "@Name";
            _name.DbType = DbType.String;
            _location = Command.CreateParameter();
            _location.ParameterName="@Location";
            _location.DbType=DbType.String;
            _infoline = Command.CreateParameter();
            _infoline.ParameterName = "@Description";
            _infoline.DbType = DbType.String;
            _disks = Command.CreateParameter();
            _disks.ParameterName="@Disks";
            _disks.DbType=DbType.String;
        }
        public BBBDElement():this(null){}
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _adapter_number.Value = AdapterNumber;
                _name.Value = Name;
                _location.Value = Location;
                _infoline.Value = InfoLine;
                _disks.Value = Disks;
                Command.Parameters.Add(_adapter_number);
                Command.Parameters.Add(_name);
                Command.Parameters.Add(_location);
                Command.Parameters.Add(_infoline);
                Command.Parameters.Add(_disks);

                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (elements[0] != "BBBD")
                {
                    _log.Error("Not BBBD Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if ((string.Compare(elements[1], "000") == 0) || (string.Compare(elements[1], "001") == 0))
                {
                    _log.Info("Description line ignored");
                    return false;
                }
                AdapterNumber = int.Parse(elements[2]);
                Name = elements[3];
                Location = elements[4];
                InfoLine = elements[5];
                Disks=string.Empty;
                for (int i=6;i<elements.Length;i++)
                {
                    Disks += elements[i] + ",";
                }
                Disks=Disks.Remove(Disks.Length - 1);
                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
